package com.ruoyi.cderp.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.TblTakeStockPlanDetail;
import com.ruoyi.cderp.mapper.TblTakeStockPlanMapper;
import com.ruoyi.cderp.domain.TblTakeStockPlan;
import com.ruoyi.cderp.service.ITblTakeStockPlanService;

/**
 * 库存盘点任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-09
 */
@Service
public class TblTakeStockPlanServiceImpl implements ITblTakeStockPlanService 
{
    @Autowired
    private TblTakeStockPlanMapper tblTakeStockPlanMapper;

    /**
     * 查询库存盘点任务
     * 
     * @param id 库存盘点任务主键
     * @return 库存盘点任务
     */
    @Override
    public TblTakeStockPlan selectTblTakeStockPlanById(String id)
    {
        return tblTakeStockPlanMapper.selectTblTakeStockPlanById(id);
    }

    /**
     * 查询库存盘点任务列表
     * 
     * @param tblTakeStockPlan 库存盘点任务
     * @return 库存盘点任务
     */
    @Override
    public List<TblTakeStockPlan> selectTblTakeStockPlanList(TblTakeStockPlan tblTakeStockPlan)
    {
        return tblTakeStockPlanMapper.selectTblTakeStockPlanList(tblTakeStockPlan);
    }

    /**
     * 新增库存盘点任务
     * 
     * @param tblTakeStockPlan 库存盘点任务
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTblTakeStockPlan(TblTakeStockPlan tblTakeStockPlan)
    {
        tblTakeStockPlan.setCreateTime(DateUtils.getNowDate());
        int rows = tblTakeStockPlanMapper.insertTblTakeStockPlan(tblTakeStockPlan);
        System.out.println(tblTakeStockPlan.getTblTakeStockPlanDetailList().size());
        insertTblTakeStockPlanDetail(tblTakeStockPlan);
        return rows;
    }

    /**
     * 修改库存盘点任务
     * 
     * @param tblTakeStockPlan 库存盘点任务
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTblTakeStockPlan(TblTakeStockPlan tblTakeStockPlan)
    {
        tblTakeStockPlan.setUpdateTime(DateUtils.getNowDate());
        tblTakeStockPlanMapper.deleteTblTakeStockPlanDetailByPlanId(tblTakeStockPlan.getId());
        insertTblTakeStockPlanDetail(tblTakeStockPlan);
        return tblTakeStockPlanMapper.updateTblTakeStockPlan(tblTakeStockPlan);
    }

    /**
     * 批量删除库存盘点任务
     * 
     * @param ids 需要删除的库存盘点任务主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblTakeStockPlanByIds(String[] ids)
    {
        tblTakeStockPlanMapper.deleteTblTakeStockPlanDetailByPlanIds(ids);
        return tblTakeStockPlanMapper.deleteTblTakeStockPlanByIds(ids);
    }

    /**
     * 删除库存盘点任务信息
     * 
     * @param id 库存盘点任务主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblTakeStockPlanById(String id)
    {
        tblTakeStockPlanMapper.deleteTblTakeStockPlanDetailByPlanId(id);
        return tblTakeStockPlanMapper.deleteTblTakeStockPlanById(id);
    }

    /**
     * 新增库存盘点任务明细信息
     * 
     * @param tblTakeStockPlan 库存盘点任务对象
     */
    public void insertTblTakeStockPlanDetail(TblTakeStockPlan tblTakeStockPlan)
    {
        List<TblTakeStockPlanDetail> tblTakeStockPlanDetailList = tblTakeStockPlan.getTblTakeStockPlanDetailList();
        String id = tblTakeStockPlan.getId();
        if (StringUtils.isNotNull(tblTakeStockPlanDetailList))
        {
            List<TblTakeStockPlanDetail> list = new ArrayList<TblTakeStockPlanDetail>();
            for (TblTakeStockPlanDetail tblTakeStockPlanDetail : tblTakeStockPlanDetailList)
            {
                tblTakeStockPlanDetail.setPlanId(id);
                list.add(tblTakeStockPlanDetail);
            }
            if (list.size() > 0)
            {
                tblTakeStockPlanMapper.batchTblTakeStockPlanDetail(list);
            }
        }
    }
}
